-- =============================================
-- Create scalar function (FN)
-- =============================================
IF EXISTS (SELECT * 
	   FROM   sysobjects 
	   WHERE  name = N'f_getbomlastcost')
	DROP FUNCTION f_getbomlastcost
GO

CREATE FUNCTION f_getbomlastcost
	(@bomid int,
	 @productid int,
	 @lastupdate datetime)
RETURNS decimal(18,8)
AS
BEGIN
  declare @lastprice decimal(18,8)
  select top 1 @lastprice=price from tt_traderprice 
    where traderid=@bomid and productid=@productid and update_time<@lastupdate
    order by update_time desc
  RETURN @lastprice
END
GO

-- =============================================
-- Example to execute function
-- =============================================


